package binarytree;

import java.util.*;

/**
 * @Author: 海琳琦
 * @Date: 2021/9/8 17:59
 */
public class Title144 {
    class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(){}
        TreeNode(int val,TreeNode left, TreeNode right){
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    static List<Integer> nodes;
    public static void main(String[] args) {
        nodes = new ArrayList<>();

    }

    /**
     * 非递归前序遍历
     * @param root
     * @return
     */
    public List<Integer> preorderTraversal(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        while(root!=null||!stack.isEmpty()){
            while(root!=null){
                nodes.add(root.val);
                stack.add(root);
                root = root.left;
            }
            TreeNode top = stack.pop();
            root = top.right;
        }
        return nodes;
    }

//    private static void preOrder(TreeNode root){
//        if(root!=null){
//            nodes.add(root.val);
//            preOrder(root.left);
//            preOrder(root.right);
//        }
//    }
}
